package com.kitty.game.achieve.common;

import com.kitty.common.cache.RedisService;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.utils.Const;
import com.kitty.game.utils.FormulaUtil;
import com.kitty.listener.event.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class SendAchieve {
    @Autowired
    RoleService roleService;
    //发送等级变更产生的成就
    public void achieveRoleLevelUpEvent(RoleLevelUpEvent roleLevelUpEvent) {
        Role role = roleLevelUpEvent.getRole();
        short level = roleLevelUpEvent.getLevel();
        if (role.isRealBody()) {
            if(level==20){
                roleService.addAchieveScore(role,"101001");
            }
            if(level==30){
                roleService.addAchieveScore(role,"101002");
            }
            if(level==40){
                roleService.addAchieveScore(role,"101003");
            }
            if(level==50){
                roleService.addAchieveScore(role,"101004");
            }
            if(level==60){
                roleService.addAchieveScore(role,"101005");
            }
            if(level==70){
                roleService.addAchieveScore(role,"101006");
            }
            if(level==80){
                roleService.addAchieveScore(role,"101007");
            }
            if(level==90){
                roleService.addAchieveScore(role,"101008");
            }
            if(level==100){
                roleService.addAchieveScore(role,"101009");
            }
            if(level==110){
                roleService.addAchieveScore(role,"101010");
            }
            if(level==120){
                roleService.addAchieveScore(role,"101011");
            }
            if(level==130){
                roleService.addAchieveScore(role,"101012");
            }

        }else{
            if(level==10){
                roleService.addAchieveScore(role,"101013");
            }
            if(level==20){
                roleService.addAchieveScore(role,"101014");
            }
            if(level==30){
                roleService.addAchieveScore(role,"101015");
            }
            if(level==40){
                roleService.addAchieveScore(role,"101016");
            }
            if(level==50){
                roleService.addAchieveScore(role,"101017");
            }
            if(level==60){
                roleService.addAchieveScore(role,"101018");
            }
            if(level==70){
                roleService.addAchieveScore(role,"101019");
            }
            if(level==80){
                roleService.addAchieveScore(role,"101020");
            }
            if(level==90){
                roleService.addAchieveScore(role,"101021");
            }
            if(level==100){
                roleService.addAchieveScore(role,"101022");
            }
            if(level==110){
                roleService.addAchieveScore(role,"101023");
            }
            if(level==120){
                roleService.addAchieveScore(role,"101024");
            }
            if(level==130){
                roleService.addAchieveScore(role,"101025");
            }
            if(level==130){
                roleService.addAchieveScore(role,"101026");
            }
        }
    }
    //发送道行变更产生的成就
    public void achieveRoleTaoEvent(TaoChangeEvent taoChangeEvent) {
        Role role = taoChangeEvent.getRole();
        int tao = taoChangeEvent.getTao();
        double stdTao = FormulaUtil.getStdTao(role.getLevel());
        //几倍标道
        for (int i = 1; i <= 30 ; i++) {
            String key = "";
            if(tao >= stdTao * i){
                if(i<=9){
                    key = "00"+i;
                }else if(i<=99){
                    key = "0"+i;
                }else{
                    key = i+"";
                }
                roleService.addAchieveScore(role,"103"+key);
            }
        }
        if(tao >= 1000*360){
            roleService.addAchieveScore(role,"103036");
        }else if(tao >= 5000*360){
            roleService.addAchieveScore(role,"103037");
        }else if(tao >= 10000*360){
            roleService.addAchieveScore(role,"103038");
        }else if(tao >= 20000*360){
            roleService.addAchieveScore(role,"103039");
        }else if(tao >= 30000*360){
            roleService.addAchieveScore(role,"103040");
        }else if(tao >= 40000*360){
            roleService.addAchieveScore(role,"103041");
        }else if(tao >= 50000*360){
            roleService.addAchieveScore(role,"103042");
        }else if(tao >= 60000*360){
            roleService.addAchieveScore(role,"103043");
        }else if(tao >= 70000*360){
            roleService.addAchieveScore(role,"103044");
        }else if(tao >= 80000*360){
            roleService.addAchieveScore(role,"103045");
        }else if(tao >= 90000*360){
            roleService.addAchieveScore(role,"103046");
        }else if(tao >= 100000*360){
            roleService.addAchieveScore(role,"103047");
        }
    }
    //发送月道行变更产生的成就
    public void achieveRoleMonthTaoEvent(MonthTaoChangeEvent monthTaoChangeEvent) {
        Role role = monthTaoChangeEvent.getRole();
        int tao = monthTaoChangeEvent.getTao();
        double stdTao = FormulaUtil.getStdTao(role.getLevel());
        //几倍标道
        for (int i = 48; i <= 57 ; i++) {
            String key = "";
            if(tao >= stdTao * (i-47)){
                if(i<=9){
                    key = "00"+i;
                }else if(i<=99){
                    key = "0"+i;
                }else{
                    key = i+"";
                }
                roleService.addAchieveScore(role,"103"+key);
            }
        }
    }
    //发送技能等级变更产生的成就
    public void achieveSkillLevelEvent(StudySkillEvent studySkillEvent) {
        Role role = studySkillEvent.getRole();
        int skillId = studySkillEvent.getSkillId();
        short skillLevel = studySkillEvent.getSkillLevel();
        //各系技能
        if(skillId < 253){
            if(skillLevel>=50){
                roleService.addAchieveScore(role,"102009");
            }
            if(skillLevel>=100){
                roleService.addAchieveScore(role,"102010");
            }
            if(skillLevel>=150){
                roleService.addAchieveScore(role,"102011");
            }
            if(skillLevel>=200){
                roleService.addAchieveScore(role,"102012");
            }
        }
        //神体术
        if(skillId == 301){
            if(skillLevel>=50){
                roleService.addAchieveScore(role,"102001");
            }
            if(skillLevel>=100){
                roleService.addAchieveScore(role,"102002");
            }
            if(skillLevel>=150){
                roleService.addAchieveScore(role,"102003");
            }
            if(skillLevel>=200){
                roleService.addAchieveScore(role,"102004");
            }
        }
        //修道术
        if(skillId == 302){
            if(skillLevel>=50){
                roleService.addAchieveScore(role,"102005");
            }
            if(skillLevel>=100){
                roleService.addAchieveScore(role,"102006");
            }
            if(skillLevel>=150){
                roleService.addAchieveScore(role,"102007");
            }
            if(skillLevel>=200){
                roleService.addAchieveScore(role,"102008");
            }
        }
        //力魄
        if(skillId == 501){
            if(skillLevel>=50){
                roleService.addAchieveScore(role,"102017");
            }
            if(skillLevel>=100){
                roleService.addAchieveScore(role,"102018");
            }
            if(skillLevel>=150){
                roleService.addAchieveScore(role,"102019");
            }
            if(skillLevel>=200){
                roleService.addAchieveScore(role,"102020");
            }
        }
    }
    //发送金钱增加产生的成就
    public void achieveRoleMoneyEvent(Role role,int money,String type) {
        if("add".equals(type)){
            if(money>=10000000){
                roleService.addAchieveScore(role,"105003");
            }
            if(money>=100000000){
                roleService.addAchieveScore(role,"105008");
            }
            if(money>=1000000000){
                roleService.addAchieveScore(role,"105002");
            }
            if(money>=2000000000){
                roleService.addAchieveScore(role,"105004");
            }
        }else{
            //消费金额
            int oldMoney = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String format = sdf.format(new Date());
            RedisService redisService = SpringUtils.getBean(RedisService.class);
            String s = redisService.get(format +role.getRoleId());
            if(StringUtils.isNotEmpty(s)){
                oldMoney = Integer.parseInt(s);
            }
            if(oldMoney>=10000000){
                roleService.addAchieveScore(role,"105006");
            }
            if(oldMoney>=100000000){
                roleService.addAchieveScore(role,"105005");
            }
            if(oldMoney>=1000000000){
                roleService.addAchieveScore(role,"105001");
            }
            if(oldMoney>=2000000000){
                roleService.addAchieveScore(role,"105007");
            }
        }


    }

    //发送装备改造改变产生的成就
    public void handleEquipChangeAchieveEvent(EquipUpgradeLevelChangeEvent equipUpgradeLevelChangeEvent) {
        Role role = equipUpgradeLevelChangeEvent.getRole();
        RoleEquip roleEquip = equipUpgradeLevelChangeEvent.getRoleEquip();
        short prevValue = equipUpgradeLevelChangeEvent.getPrevUpgradeLevlel();
        short value = equipUpgradeLevelChangeEvent.getUpgradeLevlel();
        short upgradeLevlel = equipUpgradeLevelChangeEvent.getUpgradeLevlel();
        int reqPos = roleEquip.queryEquipPos(false);
        if (reqPos == Const.WEAPON) {
            if(upgradeLevlel >=1 ){
                roleService.addAchieveScore(role,"301034");
            }
            if(upgradeLevlel >=2 ){
                roleService.addAchieveScore(role,"301004");
            }
            if(upgradeLevlel >=3 ){
                roleService.addAchieveScore(role,"301035");
            }
            if(upgradeLevlel >=4 ){
                roleService.addAchieveScore(role,"301006");
            }
            if(upgradeLevlel >=5 ){
                roleService.addAchieveScore(role,"301040");
                roleService.addAchieveScore(role,"301036");
            }
            if(upgradeLevlel >=6 ){
                roleService.addAchieveScore(role,"301007");
                roleService.addAchieveScore(role,"301050");
            }
            if(upgradeLevlel >=7 ){
                roleService.addAchieveScore(role,"301051");
                roleService.addAchieveScore(role,"301032");
            }
            if(upgradeLevlel >=8 ){
                roleService.addAchieveScore(role,"301021");
            }
            if(upgradeLevlel >=9 ){
                roleService.addAchieveScore(role,"301053");
            }
            if(upgradeLevlel >=10 ){
                roleService.addAchieveScore(role,"301042");
            }

        }else if (reqPos == Const.HELMET || reqPos == Const.ARMOR || reqPos == Const.BOOT){
            if(upgradeLevlel >=1 ){
                roleService.addAchieveScore(role,"301005");
            }
            if(upgradeLevlel >=2 ){
                roleService.addAchieveScore(role,"301033");
            }
            if(upgradeLevlel >=3 ){
                roleService.addAchieveScore(role,"301003");
            }
            if(upgradeLevlel >=4 ){
                roleService.addAchieveScore(role,"301057");
            }
            if(upgradeLevlel >=5 ){
                roleService.addAchieveScore(role,"301027");
            }
            if(upgradeLevlel >=6 ){
                roleService.addAchieveScore(role,"301054");
            }
            if(upgradeLevlel >=7 ){
                roleService.addAchieveScore(role,"301026");
            }
            if(upgradeLevlel >=8 ){
                roleService.addAchieveScore(role,"301052");
            }
            if(upgradeLevlel >=9 ){
                roleService.addAchieveScore(role,"301024");
            }
            if(upgradeLevlel >=10 ){
                roleService.addAchieveScore(role,"301008");
            }

        }
    }
    //发送穿戴装备产生的成就
    public void handleOnEquipAchieveEvent(OnEquipEvent onEquipEvent) {
        Role role = onEquipEvent.getRole();
        RoleEquip roleEquip = onEquipEvent.getRoleEquip();
        int pos = roleEquip.queryEquipPos(false);
        if (pos == Const.WEAPON || pos == Const.HELMET || pos == Const.ARMOR || pos == Const.BOOT) {
            if(roleEquip.queryLevel() == 70 ){
                roleService.addAchieveScore(role,"301014");
            }
            if(roleEquip.queryLevel() == 80 ){
                roleService.addAchieveScore(role,"301041");
            }
            if(roleEquip.queryLevel() == 90 ){
                roleService.addAchieveScore(role,"301044");
            }
            if(roleEquip.queryLevel() == 100 ){
                roleService.addAchieveScore(role,"301062");
            }
            if(roleEquip.queryLevel() == 110 ){
                roleService.addAchieveScore(role,"301011");
            }
            if(roleEquip.queryLevel() == 120 ){
                roleService.addAchieveScore(role,"301028");
            }
            if(roleEquip.queryLevel() == 130 ){
                roleService.addAchieveScore(role,"301009");
            }
        }else if(pos == Const.NECKLACE || pos == Const.BALDRIC || pos == Const.LEFT_WRIST || pos == Const.RIGHT_WRIST){
            if(roleEquip.queryLevel() == 70 ){
                roleService.addAchieveScore(role,"302004");
            }
            if(roleEquip.queryLevel() == 80 ){
                roleService.addAchieveScore(role,"302028");
            }
            if(roleEquip.queryLevel() == 90 ){
                roleService.addAchieveScore(role,"302005");
            }
            if(roleEquip.queryLevel() == 100 ){
                roleService.addAchieveScore(role,"302008");
            }
            if(roleEquip.queryLevel() == 110 ){
                roleService.addAchieveScore(role,"302014");
            }
            if(roleEquip.queryLevel() == 120 ){
                roleService.addAchieveScore(role,"302018");
            }
            if(roleEquip.queryLevel() == 130 ){
                roleService.addAchieveScore(role,"302015");
            }
        }

        int s70 = 0;
        int s80 = 0;
        int s90 = 0;
        int s100 = 0;
        int s110 = 0;
        int s120 = 0;
        int s130 = 0;

        int ss70 = 0;
        int ss80 = 0;
        int ss90 = 0;
        int ss100 = 0;
        int ss110 = 0;
        int ss120 = 0;
        int ss130 = 0;
        for (Byte equipPos : Const.MAIN_PAGE) {
            RoleEquip equip =  SpringUtils.getBean(EquipService.class).getRoleEquipByPos(role, equipPos);
            if (equip == null) {
                continue;
            }
            int reqPos = equip.queryEquipPos(false);
            if (reqPos == Const.WEAPON || reqPos == Const.HELMET || reqPos == Const.ARMOR || reqPos == Const.BOOT) {
                if(equip.queryLevel() == 70){
                    s70 +=1;
                }
                if(equip.queryLevel() == 80){
                    s80 +=1;
                }
                if(equip.queryLevel() == 90){
                    s90 +=1;
                }
                if(equip.queryLevel() == 100){
                    s100 +=1;
                }
                if(equip.queryLevel() == 110){
                    s110 +=1;
                }
                if(equip.queryLevel() == 120){
                    s120 +=1;
                }
                if(equip.queryLevel() == 130){
                    s130 +=1;
                }
            }else if(pos == Const.NECKLACE || pos == Const.BALDRIC || pos == Const.LEFT_WRIST || pos == Const.RIGHT_WRIST){
                if(equip.queryLevel() == 70){
                    ss70 +=1;
                }
                if(equip.queryLevel() == 80){
                    ss80 +=1;
                }
                if(equip.queryLevel() == 90){
                    ss90 +=1;
                }
                if(equip.queryLevel() == 100){
                    ss100 +=1;
                }
                if(equip.queryLevel() == 110){
                    ss110 +=1;
                }
                if(equip.queryLevel() == 120){
                    ss120 +=1;
                }
                if(equip.queryLevel() == 130){
                    ss130 +=1;
                }
            }
        }
        if(s70 ==4){
            roleService.addAchieveScore(role,"301017");
        }
        if(s80 ==4){
            roleService.addAchieveScore(role,"301038");
        }
        if(s90 ==4){
            roleService.addAchieveScore(role,"301013");
        }
        if(s100 ==4){
            roleService.addAchieveScore(role,"301015");
        }
        if(s110 ==4){
            roleService.addAchieveScore(role,"301030");
        }
        if(s120 ==4){
            roleService.addAchieveScore(role,"301046");
        }
        if(s130 ==4){
            roleService.addAchieveScore(role,"301012");
        }
        if(ss70 ==4){
            roleService.addAchieveScore(role,"302021");
        }
        if(ss80 ==4){
            roleService.addAchieveScore(role,"302025");
        }
        if(ss90 ==4){
            roleService.addAchieveScore(role,"302009");
        }
        if(ss100 ==4){
            roleService.addAchieveScore(role,"302007");
        }
        if(ss110 ==4){
            roleService.addAchieveScore(role,"302011");
        }
        if(ss120 ==4){
            roleService.addAchieveScore(role,"302019");
        }
        if(ss130 ==4){
            roleService.addAchieveScore(role,"302012");
        }
    }
    //法宝等级变更产生的成就
    public void handleFaBaoAchieveEvent(Role role, RoleEquip roleEquip) {
        short i = roleEquip.queryLevel();
        if(i>=2){
            roleService.addAchieveScore(role,"303015");
        }
        if(i>=3){
            roleService.addAchieveScore(role,"303025");
        }
        if(i>=4){
            roleService.addAchieveScore(role,"303012");
        }
        if(i>=5){
            roleService.addAchieveScore(role,"303048");
        }
        if(i>=6){
            roleService.addAchieveScore(role,"303035");
        }
        if(i>=7){
            roleService.addAchieveScore(role,"303023");
        }
        if(i>=8){
            roleService.addAchieveScore(role,"303007");
        }
        if(i>=9){
            roleService.addAchieveScore(role,"303043");
        }
        if(i>=10){
            roleService.addAchieveScore(role,"303039");
        }
        if(i>=11){
            roleService.addAchieveScore(role,"303014");
        }
        if(i>=12){
            roleService.addAchieveScore(role,"303026");
        }
        if(i>=13){
            roleService.addAchieveScore(role,"303038");
        }
        if(i>=14){
            roleService.addAchieveScore(role,"303050");
        }
        if(i>=15){
            roleService.addAchieveScore(role,"303009");
        }
        if(i>=16){
            roleService.addAchieveScore(role,"303027");
        }
        if(i>=17){
            roleService.addAchieveScore(role,"303037");
        }
        if(i>=18){
            roleService.addAchieveScore(role,"303049");
        }
        if(i>=19){
            roleService.addAchieveScore(role,"303005");
        }
        if(i>=20){
            roleService.addAchieveScore(role,"303041");
        }
        if(i>=21){
            roleService.addAchieveScore(role,"303003");
        }
        if(i>=22){
            roleService.addAchieveScore(role,"303029");
        }
        if(i>=23){
            roleService.addAchieveScore(role,"303017");
        }
        if(i>=24){
            roleService.addAchieveScore(role,"303002");
        }

    }
}
